home *** CD-ROM | disk | FTP | other *** search
/ Micromanía: 150 Juegos 2010 / 150Juegos_16.iso / Shareware / Shape Smash / shape-smash.swf / scripts / mx / managers / layoutClasses / PriorityQueue.as
Encoding:
Text File  |  2010-05-14  |  6.1 KB  |  219 lines

  1. package mx.managers.layoutClasses
  2. {
  3.    import flash.display.DisplayObject;
  4.    import flash.display.DisplayObjectContainer;
  5.    import mx.core.IChildList;
  6.    import mx.core.IRawChildrenContainer;
  7.    import mx.core.mx_internal;
  8.    import mx.managers.ILayoutManagerClient;
  9.    
  10.    use namespace mx_internal;
  11.    
  12.    public class PriorityQueue
  13.    {
  14.       mx_internal static const VERSION:String = "2.0.1.0";
  15.       
  16.       private var maxPriority:int = -1;
  17.       
  18.       private var arrayOfArrays:Array;
  19.       
  20.       private var minPriority:int = 0;
  21.       
  22.       public function PriorityQueue()
  23.       {
  24.          arrayOfArrays = [];
  25.          minPriority = 0;
  26.          maxPriority = -1;
  27.          super();
  28.       }
  29.       
  30.       public function addObject(param1:Object, param2:int) : void
  31.       {
  32.          if(!arrayOfArrays[param2])
  33.          {
  34.             arrayOfArrays[param2] = [];
  35.          }
  36.          arrayOfArrays[param2].push(param1);
  37.          if(maxPriority < minPriority)
  38.          {
  39.             minPriority = maxPriority = param2;
  40.          }
  41.          else
  42.          {
  43.             if(param2 < minPriority)
  44.             {
  45.                minPriority = param2;
  46.             }
  47.             if(param2 > maxPriority)
  48.             {
  49.                maxPriority = param2;
  50.             }
  51.          }
  52.       }
  53.       
  54.       public function removeSmallest() : Object
  55.       {
  56.          var _loc1_:Object = null;
  57.          _loc1_ = null;
  58.          if(minPriority <= maxPriority)
  59.          {
  60.             while(!arrayOfArrays[minPriority] || arrayOfArrays[minPriority].length == 0)
  61.             {
  62.                ++minPriority;
  63.                if(minPriority > maxPriority)
  64.                {
  65.                   return null;
  66.                }
  67.             }
  68.             _loc1_ = arrayOfArrays[minPriority].shift();
  69.             while(!arrayOfArrays[minPriority] || arrayOfArrays[minPriority].length == 0)
  70.             {
  71.                ++minPriority;
  72.                if(minPriority > maxPriority)
  73.                {
  74.                   break;
  75.                }
  76.             }
  77.          }
  78.          return _loc1_;
  79.       }
  80.       
  81.       public function removeLargestChild(param1:ILayoutManagerClient) : Object
  82.       {
  83.          var _loc2_:Object = null;
  84.          var _loc3_:int = 0;
  85.          var _loc4_:int = 0;
  86.          var _loc5_:int = 0;
  87.          _loc2_ = null;
  88.          _loc3_ = maxPriority;
  89.          _loc4_ = param1.nestLevel;
  90.          while(_loc4_ <= _loc3_)
  91.          {
  92.             if(Boolean(arrayOfArrays[_loc3_]) && arrayOfArrays[_loc3_].length > 0)
  93.             {
  94.                _loc5_ = 0;
  95.                while(_loc5_ < arrayOfArrays[_loc3_].length)
  96.                {
  97.                   if(contains(DisplayObject(param1),arrayOfArrays[_loc3_][_loc5_]))
  98.                   {
  99.                      _loc2_ = arrayOfArrays[_loc3_][_loc5_];
  100.                      arrayOfArrays[_loc3_].splice(_loc5_,1);
  101.                      return _loc2_;
  102.                   }
  103.                   _loc5_++;
  104.                }
  105.                _loc3_--;
  106.             }
  107.             else
  108.             {
  109.                if(_loc3_ == maxPriority)
  110.                {
  111.                   --maxPriority;
  112.                }
  113.                _loc3_--;
  114.                if(_loc3_ < _loc4_)
  115.                {
  116.                   break;
  117.                }
  118.             }
  119.          }
  120.          return _loc2_;
  121.       }
  122.       
  123.       public function isEmpty() : Boolean
  124.       {
  125.          return minPriority > maxPriority;
  126.       }
  127.       
  128.       public function removeLargest() : Object
  129.       {
  130.          var _loc1_:Object = null;
  131.          _loc1_ = null;
  132.          if(minPriority <= maxPriority)
  133.          {
  134.             while(!arrayOfArrays[maxPriority] || arrayOfArrays[maxPriority].length == 0)
  135.             {
  136.                --maxPriority;
  137.                if(maxPriority < minPriority)
  138.                {
  139.                   return null;
  140.                }
  141.             }
  142.             _loc1_ = arrayOfArrays[maxPriority].shift();
  143.             while(!arrayOfArrays[maxPriority] || arrayOfArrays[maxPriority].length == 0)
  144.             {
  145.                --maxPriority;
  146.                if(maxPriority < minPriority)
  147.                {
  148.                   break;
  149.                }
  150.             }
  151.          }
  152.          return _loc1_;
  153.       }
  154.       
  155.       public function removeSmallestChild(param1:ILayoutManagerClient) : Object
  156.       {
  157.          var _loc2_:Object = null;
  158.          var _loc3_:int = 0;
  159.          var _loc4_:int = 0;
  160.          _loc2_ = null;
  161.          _loc3_ = param1.nestLevel;
  162.          while(_loc3_ <= maxPriority)
  163.          {
  164.             if(Boolean(arrayOfArrays[_loc3_]) && arrayOfArrays[_loc3_].length > 0)
  165.             {
  166.                _loc4_ = 0;
  167.                while(_loc4_ < arrayOfArrays[_loc3_].length)
  168.                {
  169.                   if(contains(DisplayObject(param1),arrayOfArrays[_loc3_][_loc4_]))
  170.                   {
  171.                      _loc2_ = arrayOfArrays[_loc3_][_loc4_];
  172.                      arrayOfArrays[_loc3_].splice(_loc4_,1);
  173.                      return _loc2_;
  174.                   }
  175.                   _loc4_++;
  176.                }
  177.                _loc3_++;
  178.             }
  179.             else
  180.             {
  181.                if(_loc3_ == minPriority)
  182.                {
  183.                   ++minPriority;
  184.                }
  185.                _loc3_++;
  186.                if(_loc3_ > maxPriority)
  187.                {
  188.                   break;
  189.                }
  190.             }
  191.          }
  192.          return _loc2_;
  193.       }
  194.       
  195.       public function removeAll() : void
  196.       {
  197.          arrayOfArrays.splice(0);
  198.          minPriority = 0;
  199.          maxPriority = -1;
  200.       }
  201.       
  202.       private function contains(param1:DisplayObject, param2:DisplayObject) : Boolean
  203.       {
  204.          var _loc3_:IChildList = null;
  205.          if(param1 is IRawChildrenContainer)
  206.          {
  207.             _loc3_ = IRawChildrenContainer(param1).rawChildren;
  208.             return _loc3_.contains(param2);
  209.          }
  210.          if(param1 is DisplayObjectContainer)
  211.          {
  212.             return DisplayObjectContainer(param1).contains(param2);
  213.          }
  214.          return param1 == param2;
  215.       }
  216.    }
  217. }
  218.  
  219.